package com.situ.blog.action;

import com.opensymphony.xwork2.ActionSupport;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.Result;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@Scope("prototype")
public class UploadAction extends ActionSupport {
    private File file;
    private String fileFileName;

    public void upload() throws IOException {
        // 文件上产
        String path = "D:/mypic/";
        //png
        String extension = FilenameUtils.getExtension(fileFileName);
        //851fee51ae5d4baf9b7b32aefac508c1
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //851fee51ae5d4baf9b7b32aefac508c1.png
        String fileName = uuid + "." + extension;

        // 在upload文件夹下面创建 一个名字叫fileUploadFileName文件
        File f = new File(path, fileName);
        // 将上传的文件拷贝到目标文件夹中
        FileUtils.copyFile(file, f);

        JSONUtil.toJSON(Result.ok("上传成功", fileName));
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
}
